package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.UtilTool;
import com.ruoyi.system.domain.WebCate;
import com.ruoyi.system.domain.WebSite;
import com.ruoyi.system.mapper.WebCateMapper;
import com.ruoyi.system.mapper.WebSiteMapper;
import com.ruoyi.system.service.CommoneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jiebin
 * @date 2021/7/14 11:57
 */
@Slf4j
@Service
public class CommoneServiceImpl implements CommoneService {

    @Resource
    private WebSiteMapper webSiteMapper;

    @Resource
    private WebCateMapper webCateMapper;
    /**
     * 获取网站表id
     * @return
     */
    @Override
    public String getWebId() {
        WebSite webSite = webSiteMapper.getWebSiteById();
        if (UtilTool.isNull(webSite)){
            return "pth00000001";
        }else {
            String id = "pth"+UtilTool.haoAddOne(webSite.getId().substring(3));
            return id;
        }
    }

    /**
     * 更新网址数
     * @param cateId
     * @return
     */
    @Override
    public int updateWebCateNum(long cateId) {
        WebCate webCate = webCateMapper.selectWebCateById(cateId);
        if (UtilTool.isNull(webCate)){
            return 1;
        }else {
            WebSite webSite = new WebSite();
            webSite.setCateId(cateId);
            List<WebSite> webSites = webSiteMapper.selectWebSiteList(webSite);
            webCate.setWebSiteNum(webSites.size());
            webCateMapper.updateWebCate(webCate);
            return 0;
        }
    }

    public int updateWebCateNum(){
        List<WebCate> webCates = webCateMapper.selectWebCateList(new WebCate());
        for (WebCate cate:webCates){
            WebSite webSite = new WebSite();
            webSite.setCateId(cate.getId());
            List<WebSite> webSites = webSiteMapper.selectWebSiteList(webSite);
            cate.setWebSiteNum(webSites.size());
            webCateMapper.updateWebCate(cate);
        }
        return 0;
    }
}
